package eMdP.estats;

import java.awt.Font;
import java.io.StringReader;

import javax.swing.text.AttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.CSS;
import javax.swing.text.html.StyleSheet;

import com.jmex.bui.BButton;
import com.jmex.bui.BLabel;
import com.jmex.bui.event.ActionEvent;
import com.jmex.bui.event.ActionListener;
import com.jmex.bui.text.HTMLView;
import com.jmex.bui.util.Point;

import eMdP.classesBasiques.RegistrePuntuacio;
import eMdP.gestors.GestorConstants;
import eMdP.gestors.GestorFitxers;
import eMdP.gestors.GestorPantalla;

public class MaximesPuntuacions extends GbuiGameState {
	
	private BLabel text;
	private BButton boto;
	
	public MaximesPuntuacions(String nom)
	{
		super(nom);
		
		//window.setSize((int)(0.8f * 1.2f * display.getWidth() / 2),(int)(1.4f * (display.getHeight() / 2)));
		window.setSize(display.getWidth(), display.getHeight());
		
		int posicioX = 0;
		int posicioY = window.getHeight()-GestorConstants.ALT_BOTO-5;
		
		text = new BLabel("Màximes puntuacions");
		text.setPreferredSize(window.getWidth(), GestorConstants.ALT_BOTO);
		window.add(text, new Point(posicioX, posicioY));
		
		posicioY = posicioY - GestorConstants.ALT_BOTO-5;
		
		/*text = new BLabel("Per paquets");
		text.setPreferredSize(window.getWidth()/2, GestorConstants.ALT_BOTO);
		window.add(text, new Point(posicioX, posicioY));*/
		
		RegistrePuntuacio[] registres = GestorFitxers.llegirPerPaquets();
		
		String html = "<html><body><table>";
		html += "<tr><th><b>Per paquets (temps necessitat)</b></th>" +
				"<th><b>Per temps (paquets entrats)</b></th><tr><td></td><td></td></tr>";
		for(int i = 0; i<registres.length;i++)
		{
			html += "<tr><td>"+registres[i].getNomPersonatge()+" - "+registres[i].getNomEscenari()+
					" - "+registres[i].getValorPuntuacio()+"</td><td>En Pastanaga - Tres Cases - "+(10-i)+"</td></tr>";
		}
		
		html += "</table></body></html>";
		
		posicioY = window.getHeight()-GestorConstants.ALT_BOTO-5;
		HTMLView view = new HTMLView();
		view.setStyleSheet(crearFullEstils());
		view.setPreferredSize(window.getWidth(), window.getHeight()-GestorConstants.ALT_BOTO*2-5*2);
        view.setContents(html);
        window.add(view, new Point (posicioX+10, 0));
		
		// Tornar enrera
		 boto = new BButton("Tornar");
         boto.setPreferredSize(window.getWidth()/4, GestorConstants.ALT_BOTO);
         boto.addListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent event) {
 				GestorPantalla.canvia(7, 0);
 			}
         });
 		window.add(boto, new Point(window.getWidth()/8*3, 20));
	}
	
	private StyleSheet crearFullEstils()
	{
		StyleSheet sheet = new StyleSheet() {
            @Override
            public Font getFont(AttributeSet attrs) {
                // Java's style sheet parser annoyingly looks up whatever is
                // supplied for font-family and if it doesn't map to an
                // internal Java font; it discards it. Thanks! So we do this
                // hackery with the font-variant which it passes through
                // unmolested.
                String variant = (String)
                        attrs.getAttribute(CSS.Attribute.FONT_VARIANT);
                if (variant != null && variant.equalsIgnoreCase("Test")) {
                    int testStyle = Font.PLAIN;
                    if (StyleConstants.isBold(attrs)) {
                        testStyle |= Font.BOLD;
                    }
                    if (StyleConstants.isItalic(attrs)) {
                        testStyle |= Font.ITALIC;
                    }
                    int size = StyleConstants.getFontSize(attrs);
                    if (StyleConstants.isSuperscript(attrs) ||
                        StyleConstants.isSubscript(attrs)) {
                        size -= 2;
                    }
                    return new Font("Serif", testStyle, size);
                }
                return super.getFont(attrs);
            }
        };
        try {
            String styledef = "body { " +
                              "color: #FFFFFF;" +
                              "font-size: 16; }" +
                              "table {" +
                              "width: 90%;" +
                              "text-align: center; }";
            sheet.loadRules(new StringReader(styledef), null);
        } catch (Throwable t) {
            t.printStackTrace(System.err);
            System.out.println("No agafa el full d'estils");
        }
        
        return sheet;
	}

}
